package com.easylinkin.linkappapi.gaodemap.util;

import com.alibaba.fastjson.JSONObject;
import com.easylinkin.linkappapi.gaodemap.entity.InverseCodingDefaultResult;
import com.easylinkin.linkappapi.gaodemap.entity.InverseCodingSingleResult;
import com.easylinkin.linkappapi.gaodemap.entity.RegeoCode;
import com.easylinkin.linkappapi.openapi.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpGet;

import java.util.*;
import java.util.concurrent.Callable;

/**
 * class info :
 *
 * @author liuqihang
 * @date 2021/6/24 19:06
 */
@Slf4j
public class InverseCodingTask implements Callable<Map<String, String>> {

    private String reqLocation;

    /**
     * true 为批量请求，false为单个请求
     */
    private boolean batch;

    private String key;

    private String wepApiUrl;

    public InverseCodingTask(String reqLocation, boolean batch, String key, String wepApiUrl) {
        this.reqLocation = reqLocation;
        this.batch = batch;
        this.key = key;
        this.wepApiUrl = wepApiUrl;
    }

    @Override
    public Map<String, String> call() throws Exception {
        String[] split = reqLocation.split("\\|");
        Map<String, String> stringStringMap = sendHttpReq(reqLocation, batch, Arrays.asList(split));
        return stringStringMap;
    }

    private Map<String, String> sendHttpReq(String reqLocation, boolean batch, List<String> locationList){
        String url = wepApiUrl + HttpClientUtil.asUrlParams(appendParams(reqLocation, batch));
        HttpGet httpGet = new HttpGet(url);
        String result = HttpClientUtil.doGet(httpGet);
        List<RegeoCode> regeoCodeList = new ArrayList<RegeoCode>();
        if(batch){
            InverseCodingDefaultResult inverseCodingResult = JSONObject.parseObject(result, InverseCodingDefaultResult.class);
            if(inverseCodingResult.getStatus().equals(0)){
                return null;
            }
            regeoCodeList.addAll(inverseCodingResult.getRegeocodes());
            inverseCodingResult = null;
        }else {
            InverseCodingSingleResult inverseCodingResult = JSONObject.parseObject(result, InverseCodingSingleResult.class);
            if(inverseCodingResult.getStatus().equals(0)){
                return null;
            }
            List<RegeoCode> regeoCodeList2 = new ArrayList<RegeoCode>();
            regeoCodeList2.add(inverseCodingResult.getRegeocodes());
            regeoCodeList.addAll(regeoCodeList2);
        }
        return convertResultToMap(regeoCodeList, locationList);
    }

    private Map<String, String> convertResultToMap(List<RegeoCode> regeocodes, List<String> locationList){
        if(locationList.size() !=regeocodes.size()){
            return null;
        }
        Map<String, String> positionLocationMap =  new LinkedHashMap<>();
        for (int i = 0, size = locationList.size(); i < size; i++) {
            positionLocationMap.put(locationList.get(i), regeocodes.get(i).getFormattedAddress());
        }
        return  positionLocationMap;
    }

    private Map<String, Object> appendParams(String reqLocation, boolean batch){
        Map<String, Object> parametersMap = new HashMap<>(20);
        parametersMap.put("location", reqLocation);
        parametersMap.put("batch", batch);
        parametersMap.put("key", key);
        return parametersMap;
    }
}
